package com.zghhost.codedemo.moviedownload;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author guohua.zhang@zhaopin.com.cn
 * @Date 2019/4/24 13:46
 */
public class Chongmingming {
    public static void main(String[] args)throws Exception{
        String localpath = "/Users/wow/Downloads/fulian4/";
        File dir = new File(localpath);

        List<String> list = new ArrayList<String>();
        for(File f : dir.listFiles()){
            list.add(f.getName().split("\\.")[0]);
        }
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                Long l1 = Long.valueOf(o1.replace("cee48d64ffa",""));
                Long l2 = Long.valueOf(o2.replace("cee48d64ffa",""));
                return l1.compareTo(l2);
            }
        });

        File script = new File(localpath+"merge.sh");
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(script)));
        bw.write("#!/bin/sh\n");
        for(String l : list){
//            Runtime.getRuntime().exec("/bin/cat "+localpath+l+".ts >> "+localpath+"new.ts");
            bw.write("cat "+localpath+l+".ts >> "+localpath+"new.ts\n");
        }
        bw.flush();
        bw.close();

//        Arrays.stream(dir.listFiles()).forEach(f -> {
//            try{
//                FileChannel inputChannel = null;
//                FileChannel outputChannel = null;
//                try {
//                    inputChannel = new FileInputStream(f).getChannel();
//                    outputChannel = new FileOutputStream(localpath+f.getName().substring(4)).getChannel();
//                    outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
//                } finally {
//                    inputChannel.close();
//                    outputChannel.close();
//                }
//
//                f.delete();
//            }catch (Exception e){
//                e.printStackTrace();
//            }
//        });
    }
}
